package com.example.screenbrightness;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.widget.TextView;

public class ScreenBrightnessActivity extends Activity {
//  /** Called when the activity is first created. */
//  @Override
//  public void onCreate(Bundle savedInstanceState) {
//      super.onCreate(savedInstanceState);
//      setContentView(R.layout.main);
//  }
	
	private TextView txtStatus;
	  private RefreshHandler mRedrawHandler = new RefreshHandler();
	
	  class RefreshHandler extends Handler {
	    @Override
	    public void handleMessage(Message msg) {
	    	ScreenBrightnessActivity.this.updateUI();
	    }
	
	    public void sleep(long delayMillis) {
	      this.removeMessages(0);
	      sendMessageDelayed(obtainMessage(0), delayMillis);
	    }
	  };
	
	  private void updateUI(){
	    int currentInt = Integer.parseInt((String) txtStatus.getText()) + 20;
	    if(currentInt <= 250){
	      mRedrawHandler.sleep(1000);
	      setBrightness(currentInt);
	      txtStatus.setText(String.valueOf(currentInt));
	    }
	  }
	
	  @Override 
	  public void onCreate(Bundle icicle) { 
	    super.onCreate(icicle); 
	    setContentView(R.layout.main);
	    this.txtStatus = (TextView) this.findViewById(R.id.txtStatus);
	    updateUI();
	  } 
	
	private void setBrightness(int brightness) {
		try {
	    	IHardwareService hardware = IHardwareService.Stub.asInterface(
	    		  ServiceManager.getService("hardware"));
			if (hardware != null) {
			  hardware.setScreenBacklight(brightness);
			}
	    } catch (RemoteException doe) {          
	    }        
	}
}